<?php
/**
 * 公共组件，Table List
 *
 * @var \Ke\Web\Web     $web
 * @var \Ke\Web\Http    $http
 * @var \Ke\Web\Html    $html
 * @var \Ke\Web\Context $this
 */

use Ke\Web\Html;

//$html = $this->html;
$rows = $rows ?? [];
$rowsCount = 0;

$caption = $caption ?? '';

$rowsType = false;
if (is_array($rows)) {
	$rowsType = Html::DATA_ARRAY;
	$rowsCount = count($rows);
}
elseif ($rows instanceof \ArrayObject) {
	$rowsType = Html::DATA_ARRAY_ACCESS;
	$rowsCount = $rows->count();
}
elseif ($rows instanceof \Generator) {
	$rowsType = Html::DATA_GENERATOR;
	$rowsCount = 1;
}

if ($rowsType === false) {
	$html->message($html->getText(Html::TABLE_EMPTY), Html::MSG_WARN);
	return true;
}

// 取出数据的第一行，作为一个数据参考
if ($rowsType === Html::DATA_GENERATOR)
	$firstRow = $rows->current();
else {
	$firstRow = $rows[0] ?? [];
	if (is_object($firstRow))
		$firstRow = (array)$firstRow;
}

$checkbox = $checkbox ?? false;
if (!isset($firstRow[$checkbox]) && !isset($firstRow->{$checkbox}))
	$checkbox = false;

$checked = $checked ?? [];
if (!is_array($checked))
	$checked = (array)$checked;
$checked = array_values($checked);
$checked = array_combine($checked, $checked);

$pagination = $pagination ?? null;

$columnsType = Html::TABLE_HEAD_FROM_COLUMNS;
$columnsCount = 0;
if (!isset($columns)) {
	$columnsType = Html::TABLE_HEAD_FROM_DATA;
	$columns = $firstRow;
}
if (!is_array($columns))
	$columns = [];

$prefix = $prefix ?? '';
$modelColumns = [];
if ($rows instanceof \Ke\Adm\DataList) {
	/** @var \Ke\Adm\Model|null $model */
	$model = $rows->getModel();
	if (!empty($model) && is_subclass_of($model, \Ke\Adm\Model::class)) {
		$modelColumns = $model::getStaticColumns();
		list($ns, $cls) = parse_class($model);
		$prefix = class2id($cls);
	}
	if (!isset($pagination))
		$pagination = $rows->getPagination();
}
if (!empty($prefix))
	$prefix .= '_table';
else
	$prefix = 'table_list';

$tail = $tail ?? false;
if (!is_callable($tail))
	$tail = false;

$onRow = $onRow ?? false;
if (!is_callable($onRow))
	$onRow = false;

$attr = $attr ?? [];
if (!empty($attr) && !is_array($attr))
	$attr = $html->attr2array($attr);

if (!isset($attr['id']))
	$attr['id'] = $html->autoId($prefix);

$attr['data-ui'] = 'table-list';

$html->addClass($attr, $html->getBaseClass('table', 'list'));

$checkboxClass = $html->getBaseClass('input', 'checkbox');

?>
<div class="table-fixed">
<table <?php print $html->attr($attr); ?>>
	<?php if (!empty($caption)) { ?>
		<caption>
			<?php print $caption; ?>
		</caption>
	<?php } ?>
	<thead>
	<tr>
		<?php
		if ($checkbox) {
			$columnsCount += 1;
			?>
			<th><input type="checkbox" class="<?php print $checkboxClass; ?>" data-field="<?php print $checkbox ?>"
			           data-ui="check-all"/></th>
			<?php
		}
		?>
		<?php if (empty($columns)) { ?>
			<th><?php print $html->getText(Html::TABLE_HEAD_EMPTY); ?></th>
			<?php
		}
		else {
			foreach ($columns as $field => $column) {
				$column = $html->mkTableColumn($field, $column, $columnsType, $modelColumns[$field] ?? null);
				$columns[$field] = $column;
				if (isset($column['showTable']) && $column['showTable'] === false)
					continue;
				$columnsCount += 1;
				$cellClass = $column['class'] ?? '';
				?>
				<th <?php print $cellClass ?>data-field="<?php print $field; ?>"><?php
					print $column['label'];
					?></th>
				<?php
			}
		}
		?>
		<?php if ($tail !== false) {
			?>
			<th class="<?php print $tailClass ?? '' ?>"><?php print $html->getText(Html::TABLE_HEAD_TAIL); ?></th>
			<?php
		}
		?>
	</tr>
	</thead>
	<tbody>
	<?php
	if (empty($rows)) {
		?>
		<tr>
			<td colspan="<?php print $columnsCount; ?>">
				<?php print $html->message($html->getText(Html::TABLE_EMPTY), Html::MSG_WARN); ?>
			</td>
		</tr>
		<?php
	}
	else {
		$treeSort = null;
		$treeSortRows = null;
		if ($rows instanceof \Ke\Adm\DataList) {
			$treeSort = $rows->getTreeSort();
			if ($treeSort !== null) {
				$treeSortRows = [];
				foreach ($treeSort->getPaginateIds() as $id) {
					if (isset($rows[$id]))
						$treeSortRows[$id] = $rows[$id];
				}
				$pagination = $treeSort->getPagination();
			}
		}
		foreach ($treeSortRows ?? $rows as $index => $row) {
			$clone = new ArrayObject((array)$row);
			if ($onRow) {
				call_user_func($onRow, $html, $row, $clone);
			}
			?>
			<tr>
				<?php if ($checkbox) { ?>
					<?php
					$value = null;
					$disabledAttr = '';
					$checkedAttr = '';
					if (isset($row[$checkbox]))
						$value = $row[$checkbox];
					elseif (isset($row->{$checkbox}))
						$value = $row->{$checkbox};
					else {
						$disabledAttr = ' disabled="disabled"';
					}
					if (empty($disabledAttr)) {
						if (isset($checked[$value]))
							$checkedAttr = ' checked="checked"';
					}
					?>
					<td><input type="checkbox" class="<?php print $checkboxClass; ?>"
					           data-field="<?php print $checkbox ?>"
					           data-value="<?php print $value; ?>"
					           data-ui="check-row"<?php print $disabledAttr . $checkedAttr ?>
						/></td>
				<?php } ?>
				<?php
				foreach ($columns as $field => $column) {
					if (isset($column['showTable']) && $column['showTable'] === false)
						continue;
					$value = $clone[$field] ?? '';
					$value = $html->filterColumnValue($row, $field, $value, $column);
					?>
					<td<?php print ($column['class'] ?? ''); ?>
						data-field="<?php print $field ?>"
						data-value="<?php print ($row[$field] ?? null); ?>"><?php
						print ($value);
						?></td>
					<?php
				}
				?>
				<?php if ($tail !== false) {
					?>
					<td class="<?php print $tailClass ?? '' ?>"><?php call_user_func($tail, $html, $row, $clone); ?></td>
					<?php
				}
				?>
			</tr>
			<?php
		}
	}
	?>
	</tbody>
</table>
</div>
<?php print $html->paginate($pagination); ?>